﻿using System.Windows.Input;
using DDU.DataModel;
using DDU.Library;
using System.Linq;
using System.Windows;

namespace DDU.ViewModel
{
    public class ActivityEditViewModel : MainViewModel
    {
        void ActivityToEdit_ValueChanged(object sender, PropertyArgs e)
        {
            RaisePropertyChanged(() => TimeError);
        }

        private string guid;
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
                ActivityToEdit = Database.CollectionOfActivities.FirstOrDefault(a => a.Guid == Guid);
                RaisePropertyChanged(() => ActivityToEdit);
                RaisePropertyChanged(() => UpdateCommand);
            }
        }

        private ActivityInfo activityToEdit;
        public ActivityInfo ActivityToEdit
        {
            get { return activityToEdit; }
            set
            {
                activityToEdit = value;
                RaisePropertyChanged(() => ActivityToEdit);
                RaisePropertyChanged(() => TimeError);
                ActivityToEdit.ValueChanged -= ActivityToEdit_ValueChanged;
                ActivityToEdit.ValueChanged += ActivityToEdit_ValueChanged;

            }
        }

        public bool TimeError
        {
            get {
                if (ActivityToEdit == null)
                {
                    return false;
                }
                return ActivityToEdit.Duration < 0; }
        }

        private void Update(ActivityInfo entity)
        {
            ActivityInfo item = Database.CollectionOfActivities.FirstOrDefault(a => a.Guid == Guid);
            item = entity;
            Submit();
        }

        private void Remove(ActivityInfo entity)
        {
            Database.CollectionOfActivities.DeleteOnSubmit(entity);
            Submit();
            Navigate(BackUri);
        }

        public ICommand UpdateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return ActivityToEdit != null && !string.IsNullOrWhiteSpace(ActivityToEdit.Title) && !TimeError; },
                    ExecuteCallback = delegate
                    {
                        Update(ActivityToEdit);
                        MessageBox.Show("更新成功!");
                        Navigate(BackUri);
                    }
                };
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new GenericCommand()
                {
                    //CanExecuteCallback = delegate { return !string.IsNullOrWhiteSpace(ActivityToAdd.Title) && !TimeError; },
                    ExecuteCallback = delegate
                    {
                        if (MessageBox.Show("确定要删除这条信息吗？", "删除确认", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            Remove(ActivityToEdit);
                            Navigate(BackUri);
                        }
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Navigate(BackUri);
                    }
                };
            }
        }
    }
}
